package com.mlink.mapper;


import com.mlink.entity.RecordCountEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;


public interface RecordCountMapper {

    //  -1  标示全部

	// 获取日志列表信息
	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,r.working_hour, u.nick_name, p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			" <where> " +
			" <if test=\"userID != -1\">r.user_id=#{userID}</if> " +
			" <if test=\"projectID != -1\"> AND r.project_id=#{projectID}</if> " +
			" <if test=\"date != null\"> AND r.date=#{date}</if> " +
			" </where> " +
			" ORDER BY r.date DESC  limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordCountEntity> getRecordDay(@Param("start") Integer start, @Param("num") Integer num, @Param("userID") Integer userID, @Param("projectID") Integer projectID, @Param("date") String date);


	// 获取 项目选择all  人员选择all

	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,SUM(r.working_hour) as working_hour, u.nick_name, p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			" <if test=\"tjzq == 1\">WHERE YEARWEEK(r.date,1) = YEARWEEK(#{date},1) </if> " +
			" <if test=\"tjzq == 2\"> WHERE YEAR(date) = YEAR(#{date}) AND  MONTH(date) = MONTH(#{date})</if> " +
			" <if test=\"tjzq == 3\"> WHERE YEAR(date) = YEAR(#{date}) </if> " +
			"GROUP BY p.id,u.id "+
			" <if test=\"sort == 0\">ORDER BY u.name</if> " +
			" <if test=\"sort == 1\"> ORDER BY p.name</if> " +
			" limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordCountEntity> getRecordAllSel(@Param("start") Integer start, @Param("num") Integer num, @Param("userID") Integer userID, @Param("projectID") Integer projectID, @Param("date") String date,  @Param("tjzq") Integer tjzq, @Param("sort") Integer sort);




	 // 获取选择项目  人员all
	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,SUM(r.working_hour) as working_hour, u.nick_name,p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			" <if test=\"tjzq == 1\">WHERE YEARWEEK(r.date,1) = YEARWEEK(#{date},1) </if> " +
			" <if test=\"tjzq == 2\"> WHERE YEAR(date) = YEAR(#{date}) AND  MONTH(date) = MONTH(#{date})</if> " +
			" <if test=\"tjzq == 3\"> WHERE YEAR(date) = YEAR(#{date}) </if> " +
			" AND  p.id = #{projectID} " +
			"GROUP BY u.id "+
			" limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordCountEntity> getRecordProSel(@Param("start") Integer start, @Param("num") Integer num, @Param("userID") Integer userID, @Param("projectID") Integer projectID, @Param("date") String date,  @Param("tjzq") Integer tjzq);


	// 获取选人员 项目all
	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,SUM(r.working_hour) as working_hour, u.nick_name,p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			" <if test=\"tjzq == 1\">WHERE YEARWEEK(r.date,1) = YEARWEEK(#{date},1) </if> " +
			" <if test=\"tjzq == 2\"> WHERE YEAR(date) = YEAR(#{date}) AND  MONTH(date) = MONTH(#{date})</if> " +
			" <if test=\"tjzq == 3\"> WHERE YEAR(date) = YEAR(#{date}) </if> " +
			" AND  r.user_id = #{userID} " +
			"GROUP BY p.id "+

			" limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordCountEntity> getRecordUserSel(@Param("start") Integer start, @Param("num") Integer num, @Param("userID") Integer userID, @Param("projectID") Integer projectID, @Param("date") String date,  @Param("tjzq") Integer tjzq);

	// 获取选人员 项目
	@Select("<script> " +
			"SELECT r.id, r.user_id, r.project_id,r.date,SUM(r.working_hour) as working_hour, u.nick_name,p.name as projectName " +
			"FROM record  as  r " +
			"LEFT JOIN project p  " +
			"ON r.project_id =p.id " +
			"LEFT JOIN users u " +
			"ON r.user_id=u.id  " +
			" <if test=\"tjzq == 1\">WHERE YEARWEEK(r.date,1) = YEARWEEK(#{date},1) </if> " +
			" <if test=\"tjzq == 2\"> WHERE YEAR(date) = YEAR(#{date}) AND  MONTH(date) = MONTH(#{date})</if> " +
			" <if test=\"tjzq == 3\"> WHERE YEAR(date) = YEAR(#{date}) </if> " +
			" AND  r.user_id = #{userID} AND  r.project_id = #{projectID} " +
			" limit #{start},#{num}" +
			" </script> ")
	//查询项目

	@Results({
			@Result(property = "userID",  column = "user_id"),
			@Result(property = "projectID",  column = "project_id"),
			@Result(property = "workingHour", column = "working_hour"),
			@Result(property = "nickName", column = "nick_name")

	})

	List<RecordCountEntity> getRecordUserProSel(@Param("start") Integer start, @Param("num") Integer num, @Param("userID") Integer userID, @Param("projectID") Integer projectID, @Param("date") String date,  @Param("tjzq") Integer tjzq);



}